package com.nwpu.coursesupport.service.imple;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.RoleType;
import com.nwpu.coursesupport.pagination.PageBounds;
import com.nwpu.coursesupport.service.AbstractService;
import com.nwpu.coursesupport.view.TaskPersonalListAssistantView;
import com.nwpu.coursesupport.view.criteria.TaskPersonalListAssistantViewSearchCriteria;
import com.nwpu.coursesupport.view.mapper.TaskPersonalListAssistantViewMapper;

@Service
public class TaskPersonalListAssistantViewService extends AbstractService {
	
    protected static Logger logger = LoggerFactory.getLogger(TaskPersonalListAssistantViewService.class);

	
	@Autowired
	private TaskPersonalListAssistantViewMapper taskPersonalListAssistantViewMapper;
	
	/*public JSONResult listView(IDParam p){
		
		JSONResult rst=new JSONResult();
		rst.fail();
		
		if(p==null||p.getId()==null){
			return rst.fail("参数有误");
		}
		
		int id =(int) getSession().getAttribute("id");
		RoleType roleType=(RoleType) getSession().getAttribute("roleType");
		
		TaskPersonalListAssistantViewSearchCriteria sc=new TaskPersonalListAssistantViewSearchCriteria();
		sc.setCourseId(p.getId());
		sc.setAssignedId(id);
		sc.setRoleType(roleType);
		
		List<TaskPersonalListAssistantView> list=this.taskPersonalListAssistantViewMapper.find(sc);
		
		rst.addData("list", list);
		rst.addData("courseId",sc.getCourseId());
		rst.success();
		return rst;
	}*/

	public JSONResult listViewData(TaskPersonalListAssistantViewSearchCriteria sc){
		JSONResult rst=new JSONResult();
		rst.fail();
		
		if(sc==null||sc.getCourseId()==null){
			logger.error("参数有误");
			return rst.fail("参数有误");
		}
	
		if (sc.getPageNo() <= 0){
			sc.setPageNo(1);
		}
        
        int pageSize = 20;
        int totalCount =this.taskPersonalListAssistantViewMapper.count(sc);
        int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
		if (sc.getPageNo() > pageCount){
			sc.setPageNo(pageCount);
		}
		PageBounds page = new PageBounds();
        page.setNumber(sc.getPageNo());
        page.setSize(pageSize);
        
        int id =(int) getSession().getAttribute("id");
		RoleType roleType=(RoleType) getSession().getAttribute("roleType");
		
		sc.setAssignedId(id);
		sc.setRoleType(roleType);
		
		List<TaskPersonalListAssistantView> list=this.taskPersonalListAssistantViewMapper.find(sc,page);
		
		rst.addData("list", list);
		
		rst.addData("pageNo", sc.getPageNo());
		rst.addData("allpages", pageCount);
		rst.addData("courseId",sc.getCourseId());
		
		rst.success();
		return rst;
	}
}
